package ik.xcj.hostpital.manage.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import ik.xcj.hostpital.manage.pojo.dto.DepartmentAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.DepartmentUpdateDTO;
import ik.xcj.hostpital.manage.pojo.vo.DepartmentListItemVO;
import ik.xcj.hostpital.manage.pojo.vo.DepartmentStandardVO;
import ik.xcj.hostpital.manage.service.IDepartmentService;
import ik.xcj.hostpital.manage.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理科室相关请求的控制器
 *
 * @author java@ik.xcj
 * @version 0.0.1
 */
@Slf4j
@RestController
@Api(tags = "科室管理模块")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    public DepartmentController() {
        log.debug("创建控制器类对象：DepartmentController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加科室")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid DepartmentAddNewDTO departmentAddNewDTO){
        log.debug("开始处理【添加科室】的请求，参数：{}",departmentAddNewDTO);
        departmentService.addNew(departmentAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除科室")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@Range(min = 1, max = 1000000, message = "删除科室失败，科室ID非法！")
                             @PathVariable Long id){
        log.debug("开始处理【根据id删除科室】的请求，参数：{}",id);
        departmentService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改科室数据")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@PathVariable Long id, DepartmentUpdateDTO departmentUpdateDTO) {
        log.debug("开始处理【修改科室详情】的请求，参数：{}", departmentUpdateDTO);
        departmentService.updateInfoById(id, departmentUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询科室详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "科室ID", required = true, example = "1", dataType = "long")
    public JsonResult<DepartmentStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询科室详情失败，科室ID非法！")
                                                            @PathVariable Long id) {
        log.debug("开始处理【根据id查询科室详情】的请求，参数：{}", id);
        DepartmentStandardVO departmentStandardVO = departmentService.getStandardById(id);
        return JsonResult.ok(departmentStandardVO);
    }

    @GetMapping("/page")
    @ApiOperation("查询科室列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<PageInfo<DepartmentListItemVO>> list(Integer page,Integer pageSize) {
        log.debug("开始处理【查询科室列表】的业务，无参数");
        PageInfo<DepartmentListItemVO> list = departmentService.list(page, pageSize);
        return JsonResult.ok(list);
    }

}
